package com.qimo.zzy.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.qimo.zzy.config.Annontation;
import com.qimo.zzy.entity.ResultEntity;
import com.qimo.zzy.service.UserService;
import com.qimo.zzy.tool.JSONTool;
import com.qimo.zzy.tool.MD5Tool;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@CrossOrigin//跨域
@RestController//遵循RestFul
@RequestMapping("/qimo/User/")
public class UserController {
    @Resource
    private RedisTemplate<String, String> redisTemplate;//Redis工具类
    @Resource
    UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    MD5Tool md5Tool;

    /**
     * 根据手机号查找密码
     *
     * @param phone
     * @return
     */

    @Annontation.LimitRequest(count = 5)
    @PostMapping("loginByphone")
    public String loginByphone(@RequestParam("phone") String phone, @RequestParam("pwd") String pwd) {
        String a = userService.loginByphone(phone);
        pwd = md5Tool.ToMD5(pwd);
        if (a.equals(pwd)) {
            JSONObject jo = JSON.parseObject(userService.selectByphone(phone));
            StpUtil.login(jo.getJSONObject("data").get("uid"));
            Gson gson = new Gson();
            String to=gson.toJson(StpUtil.getTokenInfo());
            JSONObject Token = JSON.parseObject(to);
            jo.put("token",Token);
            return jo.toJSONString();
        } else {
            ResultEntity resultEntity = new ResultEntity(null, null, null);
            resultEntity.setCode(900);
            resultEntity.setMsg("密码错误");
            return JSONTool.getJsonTool().toJson(resultEntity);
        }

    }

    /**
     * 根据邮箱号查询密码
     *
     * @param uemail
     * @param pwd
     * @return
     */
    @PostMapping("loginByEmail")
    @Annontation.LimitRequest(count = 5)
    public String loginByEmail(@RequestParam("uemail") String uemail,
                               @RequestParam("pwd") String pwd) {
        String a = userService.loginByEmail(uemail);
        pwd = md5Tool.ToMD5(pwd);
        if (a.equals(pwd)) {
            JSONObject jo = JSON.parseObject(userService.selectByUemail(uemail));
            StpUtil.login(jo.getJSONObject("data").get("uid"));
            Gson gson = new Gson();
            String to=gson.toJson(StpUtil.getTokenInfo());
            JSONObject Token = JSON.parseObject(to);
            jo.put("token",Token);
            return jo.toJSONString();
        } else {
            ResultEntity resultEntity = new ResultEntity(null, null, null);
            resultEntity.setCode(900);
            resultEntity.setMsg("密码错误");
            return JSONTool.getJsonTool().toJson(resultEntity);
        }

    }

    /**
     * 增加用户
     *
     * @param uemail
     * @param uphone
     * @param pwd
     * @return
     */
    @PostMapping("addUser")
    public String addUser(@RequestParam("uemail") String uemail,
                          @RequestParam("uphone") String uphone,
                          @RequestParam("pwd") String pwd) {
        boolean flag = true;
        if (!uemail.contains("@")) {
            flag = false;
        }
        if (uphone.length() != 11) {
            flag = false;
        }
        pwd = md5Tool.ToMD5(pwd);
        if (flag) {
            return userService.addUser(uemail, uphone, pwd);
        } else {
            ResultEntity resultEntity = new ResultEntity(null, null, null);
            resultEntity.setCode(900);
            resultEntity.setMsg("用户名不符合规范");
            return JSONTool.getJsonTool().toJson(resultEntity);
        }
    }

    /**
     * 一键修改为123456
     *
     * @param uemail
     * @return
     */
    @PostMapping("rePwd")
    public String rePwd(
            @RequestParam("uemail") String uemail
    ) {
        String pwd = md5Tool.ToMD5("123456");
        return userService.rePwd(uemail, pwd);
    }

    /**
     * 用户修改密码
     *
     * @param uemail
     * @param pwd
     * @return
     */
    @PostMapping("reSetPwd")
    public String reSetPwd(
            @RequestParam("uemail") String uemail,
            @RequestParam("pwd") String pwd
    ) {
        return userService.rePwd(uemail, md5Tool.ToMD5(pwd));
    }


    /**
     * 获取用户详情
     *
     * @param email 邮箱
     * @return Json
     */
    @GetMapping("getUserDetailList")
    public String getUserDetailList(@RequestParam("email") String email) {
        return userService.getUserDetailList(email);
    }

    /**
     * 新增用户详情
     *
     * @param uId
     * @param avatar
     * @param weChat
     * @param qq
     * @param sex
     * @param uname
     * @return
     */
    @PostMapping("addUserDetail")
    public String addUserDetail(
            @RequestParam("uId") int uId,
            @RequestParam("avatar") String avatar,
            @RequestParam("weChat") String weChat,
            @RequestParam("qq") String qq,
            @RequestParam("sex") int sex,
            @RequestParam("uname") String uname
    ) {
        return userService.addUserDetail(uId, avatar, weChat, qq, sex, uname);
    }

    /**
     * 验证验证码
     *
     * @param email
     * @param code
     * @return
     */
    @PostMapping("checkcode")
    public String checkcode(
            @RequestParam("email") String email,
            @RequestParam("code") String code
    ) {
        String codeInRedis = stringRedisTemplate.opsForValue().get(email);
        ResultEntity resultEntity = new ResultEntity(null, null, null);
        if (codeInRedis == null || !codeInRedis.equals(code)) {
            resultEntity.setCode(900);
            resultEntity.setMsg("验证码错误");
        } else {
            resultEntity.setCode(200);
            resultEntity.setMsg("验证通过");
            stringRedisTemplate.delete(email);
        }
        return JSONTool.getJsonTool().toJson(resultEntity);
    }

    /**
     * 修改用户详情功能
     *
     * @param uId
     * @param avatar
     * @param weChat
     * @param qq
     * @param sex
     * @param uname
     * @return
     */
    @PostMapping("chanceUserDetail")
    public String chanceUserDetail(
            @RequestParam("uId") int uId,
            @RequestParam("avatar") String avatar,
            @RequestParam("weChat") String weChat,
            @RequestParam("qq") String qq,
            @RequestParam("sex") int sex,
            @RequestParam("uname") String uname
    ) {
        return userService.chanceUserDetail(uId, avatar, weChat, qq, sex, uname);
    }

    /**
     * 软删除用户
     *
     * @param uId
     * @return
     */
    @PostMapping("changeIsDel")
    public String changeIsDel(
            @RequestParam("uId") int uId
    ) {
        return userService.changeIsDel(uId);
    }

    /**
     * 硬删除用户
     *
     * @param uId
     * @return
     */
    @PostMapping("delUser")
    public String delUser(
            @RequestParam("uId") int uId
    ) {
        return userService.delUser(uId);
    }

    /**
     * 判断是不是登录
     *
     * @return
     */
    @RequestMapping("isLogin")
    public SaResult isLogin() {
        System.out.println(StpUtil.isLogin());
        return SaResult.ok("是否登录：" + StpUtil.isLogin());
    }

    /**
     * 查询token信息
     *
     * @return
     */
    @RequestMapping("tokenInfo")
    public SaResult tokenInfo() {
        return SaResult.data(StpUtil.getTokenInfo());
    }

    /**
     * 退出登录
     *
     * @return
     */
    @RequestMapping("logout")
    public SaResult logout() {
        StpUtil.logout();
        return SaResult.ok();
    }

}
